<template>
  <div class="main">
    <vue-particles color="#dedede"></vue-particles>
    <div class="loginBox">
      <el-form class="login_form" :model="login" :rules="rules" ref="login">
        <el-form-item>
          <el-input
            placeholder="请输入用户名"
            v-model="login.username"
            type="text"
            prefix-icon="el-icon-s-custom"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            placeholder="请输入密码"
            v-model="login.password"
            type="password"
            prefix-icon="el-icon-edit"
          ></el-input>
        </el-form-item>
        <el-button class="btn" type="danger" icon="el-icon-postcard">
          注册
        </el-button>
        <el-button
          class="btn"
          type="primary"
          icon="el-icon-check"
          @click="submitForm()"
        >
          登录
        </el-button>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data: function() {
    return {
      login: {
        username: "wdf8",
        password: "123"
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm() {
      this.$refs.login.validate(valid => {
        if (valid) {
          this.$http
            .post("user/login2", {
              username: this.login.username,
              password: this.login.password
            })
            .then(response => {
              console.log(response);
              if (response.data.code !== 0) {
                this.$message.error(response.data.message);
              } else {
                window.sessionStorage.setItem(
                  "token",
                  response.headers.authorization
                );
                console.log(response.headers.authorization);
                this.$message.success("登录成功");
                this.$router.push("/home");
              }
            })
            .catch(error => {
              this.$message.error(error);
            });
        } else {
          this.$message.error("请输入账号和密码");
          //console.log(valid);
          return false;
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.loginBox {
  width: 400px;
  height: 250px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  border-radius: 40px;
  background-color: lightpink;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.main {
  background-color: hotpink;
  height: 100%;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px 20px 20px;
  box-sizing: border-box;
}
</style>
